import {defineStore} from 'pinia';
import {Http, Cache, Helper, Msg, Constant, Log} from 'core';

const store = {
    id: "order",
    state() {
        return {
            /** 订单列表 **/
            pageNo: 1,
            pages: 0,
            records: [],
            pageSize: 10,
            total: 0,
            condition:{},
            hasMore: true,

            /** 订单信息 **/
            orderInfo: {},

            orderConfig: {},
        }
    },

    getters: {},

    actions: {
        /**
         * 查询我的订单列表
         * @param keyword 手机号
         * @param orderState
         * @param startTime
         * @param endTime
         * @returns {Promise<void>}
         */
        async getList(pageNo, pageSize, condition) {
            if (pageNo == 1) {
                this.records = [];
                this.hasMore = true;
            }

            if(! this.hasMore) {
                return;
            }

            this.condition = condition;
            let params = {current: pageNo, size: pageSize};
            if(! Helper.emptyJson(this.condition)) {
                params = Object.assign(params, this.condition);
            }

            Msg.loading("加载中...");
            let {code, data} = await Http.get("/orders/myOrder/page", params);
            Msg.loading(false);
            // Log.info("获取我的订单列表", code, data);
            if (code == 200) {
                this.pages = data.pages;
                this.pageNo = data.current;
                this.pageSize = data.size;
                this.total = data.total;
                if(Helper.emptyArray(data.records)) {
                    this.hasMore = false;
                }
                this.records = this.records.concat(data.records);
            }

            return this.records;
        },

        /**
         * 订单详情
         * @param orderNo
         * @returns {Promise<void>}
         */
        async getInfo(orderNo) {
            Msg.loading("加载中...");
            this.orderInfo = {};
            let {code, data} = await Http.get(`/orders/getByOrderNo/${orderNo}`);
            Msg.loading(false);
            if(code == 200) {
                this.orderInfo = data;
            }

            return this.orderInfo;
        },

        /**
         * 创建订单
         * @param form
         * @returns {Promise<*>}
         */
        async submit(form) {
            if (!this.validate(form)) {
                return;
            }

            Msg.loading("加载中...")
            let {code, data} = await Http.post("/orders", form);
            Msg.loading(false);
            console.log(code, data, '订单结果')
            if (code == 200) {
                if (Helper.empty(data)) {
                    Msg.error("生成订单失败");
                    return;
                }

                return data;
            }
        },

        /**
         * 获取订单二维码
         * @param orderNo
         * @returns {Promise<*>}
         */
        async getQrcode(orderNo) {
            Msg.loading("加载中...")
            let {code, data} = await Http.get(`/orders/getAlipayQrcodeByOrderNo/${orderNo}`);
            Msg.loading(false);
            if (code == 200) {
                if (Helper.empty(data)) {
                    Msg.error('获取订单二维码失败');
                    return;
                }

                return data;
            }
        },

        /**
         * 获取订单扣款设置
         * @param orderNo
         * @returns {Promise<void>}
         */
        async getConfig(orderNo) {
            Msg.loading("加载中...")
            this.orderConfig = {};
            let {code, data} = await Http.get(`/orders/getOrderWithholdConfig/${orderNo}`)
            Msg.loading(false);
            if(code == 200) {
                this.orderConfig = data;
            }
            return this.orderConfig;
        },

        /**
         * 设置订单扣款设置
         * @param orderNo
         * @param userPay
         * @param userPayRate
         * @param sysWithhold
         * @param sysWithholdRate
         * @returns {Promise<boolean>}
         */
        async setConfig(orderNo, userPay, userPayRate, sysWithhold, sysWithholdRate) {
            Msg.loading("加载中...")
            let {code, data} = await Http.post(`/orders/setOrderPayConfig/${orderNo}`, {userPay, userPayRate, sysWithhold, sysWithholdRate})
            Msg.loading(false);
            if(code == 200) {
                Msg.success("设置成功");
                return true;
            }

            return false;
        },

        /**
         * 绑定接口
         * @param orderNo
         * @returns {Promise<void>}
         */
        async bindOrder(orderNo) {
            Msg.loading("加载中...")
            let {code, data} = await Http.get("/orders/appBindOrder/" + orderNo).validate(false);
            Msg.loading(false);
            if(code == 200) {
                return true;
            }
            return false;
        },

        /**
         * 签约
         * @param orderNo
         * @returns {Promise<void>}
         */
        async sign(orderNo) {
            Msg.loading("签约中....")
            let {code, data} = await Http.get("/orders/getSignParamStr/" + orderNo)
            Msg.loading(false);
            if(code == 200) {
                if(Helper.empty(data)) {
                    Msg.error("签约发起失败");
                    return "";
                }
                return data;
            }

            return "";
        },

        /**
         * 订单表单验证
         * @param form
         * @returns {boolean}
         */
        validate(form) {
            if(Helper.empty(form.storeId)) {
                Msg.error("请选择门店");
                return false;
            }

            if(Helper.empty(form.payCycle)) {
                Msg.error("请选择收款周期");
                return false;
            }

            if(Helper.empty(form.leaseTerm)) {
                Msg.error("请设置期数");
                return false;
            }

            if(form.payCycle == Constant.DAY && ! (form.leaseTerm >= 1 && form.leaseTerm <= 365)) {
                Msg.error("按日收款，请输入1~365");
                return false;
            }

            if(form.payCycle == Constant.WEEK && ! (form.leaseTerm >= 1 && form.leaseTerm <= 52)) {
                Msg.error("按日收款，请输入1~52");
                return false;
            }

            if(form.payCycle == Constant.MONTH && ! (form.leaseTerm >= 1 && form.leaseTerm <= 12)) {
                Msg.error("按日收款，请输入1~12");
                return false;
            }

            if(form.payCycle == Constant.QUARTER && ! (form.leaseTerm >= 1 && form.leaseTerm <= 4)) {
                Msg.error("按日收款，请输入1~4");
                return false;
            }

            if(Helper.empty(form.termAmount)) {
                Msg.error("请设置每期金额");
                return false;
            }

            if(Helper.empty(form.leaseStartDate)) {
                Msg.error("请选择起租日期");
                return false;
            }

            if(Helper.empty(form.depositType)) {
                Msg.error("请选择押金类型");
                return false;
            }

            return true;
        }
    }
}

export default defineStore(store)
